package com.qf.service.Impl;

import com.qf.dao.EmsDao;
import com.qf.dao.Impl.EmpDaoImpl;
import com.qf.pojo.Emp;
import com.qf.service.EmpService;
import com.qf.utils.Dbutils;

import java.util.List;

public class EmpServiceImpl implements EmpService {
    private static EmsDao empService = new EmpDaoImpl();
    @Override
    public int add(Emp emp) {
        int a =0;
        try {
            Dbutils.begin();
            a = empService.add(emp);
            Dbutils.commit();
        } catch (Exception e) {
            e.printStackTrace();
            Dbutils.rollback();
        }finally {
            Dbutils.close();
        }
        return a;
    }

    @Override
    public int delete(int id) {
        int a = 0;
        try {
            Dbutils.begin();
            a = empService.delete(id);
            Dbutils.commit();
        } catch (Exception e) {
            e.printStackTrace();
            Dbutils.rollback();
        }finally {
            Dbutils.close();
        }
        return a;
    }

    @Override
    public int update(Emp emp) {

        int a = 0;
        try {
            Dbutils.begin();
            a = empService.add(emp);
            Dbutils.commit();
        } catch (Exception e) {
            e.printStackTrace();
            Dbutils.rollback();
        }finally {
            Dbutils.close();
        }
        return a;
    }

    @Override
    public Emp select(int id) {
        try {
            Emp select = empService.select(id);
            Dbutils.close();
            return select;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage()+"查询单个异常");
        }
    }

    @Override
    public List<Emp> selectAll() {
        List<Emp> emps = empService.selectAll();
        Dbutils.close();
        return emps;
    }

    @Override
    public void fenye(int pageNum, int pageSize) {

        int pyl = (pageNum-1)*pageSize;
        List<Emp> fenye = empService.fenye(pyl, pageSize);
        System.out.println(fenye);
    }

}
